package com.accenture.portal.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.LoginLog;
import com.accenture.portal.service.LoginLogService;

@Controller
public class LoginLogController {
	@Autowired
	LoginLogService loginLogService;
	
	@RequestMapping("searchLoginInfo.action")
	public String searchTimeList(@RequestParam(value="cur", required=false)Integer cur, HttpServletRequest request,
			HttpServletResponse response,Model model) throws ParseException{
		
		Map<String,Object> loginMap = new HashMap<String,Object>();
		

		String user_name = request.getParameter("user_name");
		String status = request.getParameter("status");
		String begain_date = request.getParameter("begainDate");
		String end_date = request.getParameter("endDate");
		
		loginMap.put("user_name", user_name);
		loginMap.put("status", status);
		
		SimpleDateFormat transac = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ENGLISH);
		
		if(begain_date != null && begain_date.length() > 0){
			Date dateBegain = transac.parse(begain_date);
			loginMap.put("begain",dateBegain);
		}
		if(end_date !=null && end_date.length() > 0){
			Date dateEnd = transac.parse(end_date);
			loginMap.put("end", dateEnd);
		}
		
		//分页处理
		
		loginMap.put("cur", 0);
		loginMap.put("pageSize", 0);
		int total = loginLogService.searchLoginLogByTimeAndUserNameAndStatus(loginMap).size();//总条数
		int pageSize = 10;//每页显示10条
		int pageNumber = total/pageSize; //总页数
		
		
		if(total % pageSize != 0){
			pageNumber++;
		}
		
		if(pageNumber == 0){
			pageNumber = 1;
		}
		
		if(cur == null || cur < 1 ){
			cur = 1;
		}else if(cur > pageNumber){
			cur = pageNumber;
		}
		
		int begainData = (cur - 1) * pageSize;
		
		//将当前页开始的第一条数据的序号和每页的条数放到MAP中，传给SQL语句
		loginMap.put("begainData", begainData);
		loginMap.put("pageSize", pageSize);
		
		List<LoginLog> lgoinInfo = loginLogService.searchLoginLogByTimeAndUserNameAndStatus(loginMap);
		
		model.addAttribute("lgoinInfo", lgoinInfo);
		
		request.getSession().setAttribute("cur", cur);
		request.getSession().setAttribute("pageNumber", pageNumber);
		
		request.getSession().setAttribute("user_name", user_name);
		request.getSession().setAttribute("status", status);
		request.getSession().setAttribute("begain_date", begain_date);
		request.getSession().setAttribute("end_date", end_date);
		
		return "log_searchInfo";
	}
}
